import notification from 'ant-design-vue/es/notification'
import { src } from "./uitl"

const Login = `
mutation Login ($account: String, $password: String) {
    login (account: $account, password: $password) 
  }
`

export const login = params => {
    const { account, password } = params
    return SendGQL({
        query: Login,
        variables: {
            account, password
        }
    })
}
const remoteAddress = src.url

var SendGQL = function (queryPayloadObject) {
    return fetch(remoteAddress + '/api/graphqlVisitor', {
        method: 'POST',
        body: JSON.stringify({
            variables: queryPayloadObject.variables,
            query: queryPayloadObject.query
        }),
        headers: {
            'Content-Type': 'application/json'
        }
    }).then(response => {
        if (response.ok) {
            return response.json()
        } else {
            throw new Error('--Network Error--')
        }
    }).then(json => {
        // dig into two layer
        // tslint:disable-next-line:forin
        if (json.errors) {
            notification.error({
                message: '错误',
                description: JSON.stringify(json.errors)
            })
        }
        for (const data in json) {
            // tslint:disable-next-line:forin
            for (const f in json[data]) {
                return json[data][f]
            }
        }
    })
}
